﻿using System;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using System.Windows;
using Coding4Fun.MissionControl.WP.Misc;
using Coding4Fun.MissionControl.WP.ViewModels;

namespace Coding4Fun.MissionControl.WP.Views
{
    public partial class CommandsPage : PhoneApplicationPage
    {
        public CommandsPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            if (e.Uri.ToString().Contains("SetsPage"))
            {
                // Reset the basic ViewModel containers to ignore any delayed
                // server responses.
                CommandsPageViewModel.Instance.Commands = new System.Collections.ObjectModel.ObservableCollection<string>();
                CommonViewModel.Instance.CurrentSet = string.Empty;
                CommonViewModel.Instance.CurrentSetType = string.Empty;
            }

            base.OnNavigatingFrom(e);
        }

        private void WaitingOverlayLayoutUpdated(object sender, EventArgs e)
        {
            if (grdConnectOverlay.Visibility == System.Windows.Visibility.Collapsed)
            {
                ApplicationBar.IsVisible = true;
            }
            else
            {
                ApplicationBar.IsVisible = false;
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            CommandViewModel.Instance.HandleOverlay(e);

            base.OnBackKeyPress(e);
        }

        private async void AttemptLoadCommands()
        {
            bool commandSent = await CommonViewModel.Instance.CommandClient.SendCommand(CommonViewModel.Instance.CurrentServer.Location,
                Constants.COMMAND_GET_COMMANDS, CommonViewModel.Instance.CurrentSet);
            if (!commandSent)
            {
                Alert.Send(Constants.MESSAGE_SERVER_CONNECT_FAIL);
                CommonViewModel.Instance.IsWaiting = false;
            }
        }

        private void btnTrash_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Are you sure you want to remove this server? This will also remove all the favorites that are associated with it.",
                        "Remove Server", MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                CollectionHelper.RemoveServer(CommonViewModel.Instance.CurrentServer);

                if (NavigationService.CanGoBack)
                    NavigationService.GoBack();
            }
        }

        private void btnLearn_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/LearnCodePage.xaml", UriKind.Relative));
        }

        private async void lstCommands_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstCommands.SelectedItem != null)
            {
                string selectedItem = lstCommands.SelectedItem.ToString();

                bool commandSent = await CommonViewModel.Instance.CommandClient.SendCommand(CommonViewModel.Instance.CurrentServer.Location,
                    string.Format(Constants.COMMAND_EXECUTE, CommonViewModel.Instance.CurrentSet), selectedItem);
                if (!commandSent)
                {
                    Alert.Send(Constants.MESSAGE_SERVER_CONNECT_FAIL);
                }

                lstCommands.SelectedItem = null;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            CommonViewModel.Instance.IsWaiting = true;
            AttemptLoadCommands();
        }
    }
}